﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class orderController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static orderInfo getbyID(string orderID)
        {
            SqlCommand cmd = new SqlCommand("getIDOrder", con);
            orderInfo objOrder = new orderInfo();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@orderID", orderID));
            SqlDataReader dr = cmd.ExecuteReader(); ;
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    objOrder.orderID = dr.GetInt32(0);
                    objOrder.customerID = dr.GetInt32(1);
                    objOrder.dateorder = dr.GetDateTime(2);
                    objOrder.datereceipt = dr.GetDateTime(3);
                    objOrder.addressDelivery = dr.GetString(4);
                    objOrder.phone = dr.GetString(5);
                    objOrder.paymentID = dr.GetInt32(6);
                    objOrder.totalMoney = dr.GetString(7);
                    objOrder.status = dr.GetBoolean(8);
                    objOrder.deliveryCharges = dr.GetString(9);
                }
            }
            dr.Close();
            return objOrder;
        }

        #endregion

        #region getbyAll
        public static DataTable Listorder()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listOrder", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertCatogory(orderInfo objOrder)
        {
            using (SqlCommand cmd = new SqlCommand("insertOrder", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@customerID", objOrder.customerID));
                cmd.Parameters.Add(new SqlParameter("@dateorder", objOrder.dateorder));
                cmd.Parameters.Add(new SqlParameter("@datereceipt", objOrder.datereceipt));
                cmd.Parameters.Add(new SqlParameter("@addressDelivery", objOrder.addressDelivery));
                cmd.Parameters.Add(new SqlParameter("@phone", objOrder.phone));
                cmd.Parameters.Add(new SqlParameter("@paymentID", objOrder.paymentID));
                cmd.Parameters.Add(new SqlParameter("@totalMoney", objOrder.totalMoney));
                cmd.Parameters.Add(new SqlParameter("@status", objOrder.status));
                cmd.Parameters.Add(new SqlParameter("@deliveryCharges", objOrder.deliveryCharges));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateCatogory(orderInfo objOrder)
        {
            using (SqlCommand cmd = new SqlCommand("updateOrder", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", objOrder.orderID));
                cmd.Parameters.Add(new SqlParameter("@customerID", objOrder.customerID));
                cmd.Parameters.Add(new SqlParameter("@dateorder", objOrder.dateorder));
                cmd.Parameters.Add(new SqlParameter("@datereceipt", objOrder.datereceipt));
                cmd.Parameters.Add(new SqlParameter("@addressDelivery", objOrder.addressDelivery));
                cmd.Parameters.Add(new SqlParameter("@phone", objOrder.phone));
                cmd.Parameters.Add(new SqlParameter("@paymentID", objOrder.paymentID));
                cmd.Parameters.Add(new SqlParameter("@totalMoney", objOrder.totalMoney));
                cmd.Parameters.Add(new SqlParameter("@status", objOrder.status));
                cmd.Parameters.Add(new SqlParameter("@deliveryCharges", objOrder.deliveryCharges));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteCatogory(int orderID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteOrder", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@orderID", orderID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region checkOrder
        public static bool checkOrder(int orderID)
        {
            SqlCommand cmd = new SqlCommand("checkOrder", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@orderID", orderID));
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                return true;
            }
            dr.Close();
            return false;
        }
        #endregion
    }
}
